<?php

namespace App\Models;

class CarPropertyIndex extends Model
{
    protected $table = 'yf_car_property_index';
    protected $primaryKey = 'car_property_index_id';
    public $timestamps = false;
    protected $guarded = ['car_property_index_id'];

    public static function getIntersectCommonIdByPropertyValueId(array $property_value_ids) : array
    {
        $intersectCommonIds = [];
        self::whereIn('car_property_value_id', $property_value_ids)
            ->get(['common_id', 'car_property_value_id'])
            ->groupBy('car_property_value_id')
            ->values()
            ->each(function ($item, $index) use (&$intersectCommonIds) {
                if (!$index) {
                    $intersectCommonIds = $item->pluck('common_id')->toArray();
                } else {
                    $intersectCommonIds = array_intersect($intersectCommonIds, $item->pluck('common_id')->toArray());
                }
            });

        return $intersectCommonIds;
    }
}
